#include"HttpServer.hpp"
#define WWWROOT "./wwwroot"

//将请求组成请求格式的字符串
std::string Requeststr(const HttpRequest& req)
{
    std::stringstream ss;
    ss<< req._method << " " << req._path << " " << req._version << "\r\n";
    //查询语句
    for(auto& it : req._params) 
    {
        ss << it.first << ": " << it.second << "\r\n";
    }
    for(auto& it : req._headers)
    {
        ss << it.first << ": " << it.second << "\r\n";
    }
    //空行
    ss << "\r\n";
    //正文
    ss << req._body;
    return ss.str();
}

void Hello(const HttpRequest& req, HttpResponce*resp)
{
    //应答的上下文为请求？设置一下
    resp->SetContent(Requeststr(req) , "text/plian");
}
void Login(const HttpRequest& req, HttpResponce*resp)
{
    resp->SetContent(Requeststr(req) , "text/plian");
}
void PutFile(const HttpRequest& req, HttpResponce*resp)
{
    std::string pathname = WWWROOT + req._path;
    Util::WriteFile(pathname ,req._body);

}
void DeleteFile(const HttpRequest& req, HttpResponce*resp)
{
    resp->SetContent(Requeststr(req) , "test/plain");
}

int main()
{
    //构建服务器
    HttpServer server(8085);
    server.SetThreadCount(3);//设置线程池中的线程数量
    server.SetBaseDir(WWWROOT);//设置静态资源目录
    //设置一系列的静态资源的获取方法
    server.Get("/hello" , Hello);
    server.Post("/login" , Login);
    server.Put("/111.txt" , PutFile);
    server.Delete("/111.txt" , DeleteFile);
    //开辟服务
    server.Listen();
    return 0;
}